package com.example.higo.view.fragment.homepage;


import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.higo.R;
import com.example.higo.api.service.ExceptionHandle;
import com.example.higo.model.entity.homepage.HomePageBody;
import com.example.higo.model.entity.homepage.HomePageTab;
import com.example.higo.persenter.homepage.HomePagePersenter;
import com.example.higo.view.adapter.homepage.HomePageMainVpAdapter;
import com.example.higo.view.fragment.BaseFragment;
import com.example.higo.view.imp.homepage.HomePageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomePageFragment extends BaseFragment implements HomePageView {

    @BindView(R.id.homepager_main_tab)
    TabLayout homepagerMainTab;
    @BindView(R.id.homepager_main_vp)
    ViewPager homepagerMainVp;

    private HomePageMainVpAdapter adapter;
    private PinpaiFragment pinpaiFragment;
    private CategoryFragment categoryFragment;
    private NewGoodsFragment newGoodsFragment;

    private HomePagePersenter persenter;
    private List<String> titles = new ArrayList<>();
    private List<Fragment> data = new ArrayList<>();
    private String page = "1";

    public static HomePageFragment newInstance() {

        Bundle args = new Bundle();

        HomePageFragment fragment = new HomePageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_home_page;
    }

    @Override
    public void initViews() {
        persenter = new HomePagePersenter(this);

        adapter = new HomePageMainVpAdapter(getChildFragmentManager(), data, titles);
        homepagerMainVp.setAdapter(adapter);
        homepagerMainTab.setupWithViewPager(homepagerMainVp);
    }

    @Override
    public void loadData() {
        persenter.getHomePageTabData();
        persenter.getHomePageBodyData(page);
    }

    @Override
    public void onSuccessTab(HomePageTab homePageTab) {
        pinpaiFragment = PinpaiFragment.newInstance();
        categoryFragment = CategoryFragment.newInstance();
        newGoodsFragment = NewGoodsFragment.newInstance();

        data.add(pinpaiFragment);
        data.add(categoryFragment);
        data.add(newGoodsFragment);

        if (homePageTab != null) {
            List<HomePageTab.DataBean.TopTabsBean> topTabs = homePageTab.getData().getTopTabs();
            for (int i = 0; i < topTabs.size(); i++) {
                String text = topTabs.get(i).getText();
                titles.add(text);
            }
        }

        adapter.notifyDataSetChanged();

    }

    @Override
    public void onSuccessBody(HomePageBody homePageBody) {
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    /**
     * 点击事件
     */
    @OnClick({R.id.homepager_main_search_iv,R.id.homepager_main_shopcar_iv})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.homepager_main_search_iv:
                Snackbar.make(v,"search",Snackbar.LENGTH_SHORT);
                break;
            case R.id.homepager_main_shopcar_iv:
                Snackbar.make(v,"shopcar",Snackbar.LENGTH_SHORT);
                break;
        }
    }

}
